package edu.cmu.cs.stage3.alice.core.behavior;

import edu.cmu.cs.stage3.alice.core.Expression;
import edu.cmu.cs.stage3.alice.core.property.BooleanProperty;

public class ConditionalTriggerBehavior extends TriggerBehavior {
	private static Class[] s_supportedCoercionClasses = { ConditionalBehavior.class };
	//Rem @Override
	public Class[] getSupportedCoercionClasses() {
		return s_supportedCoercionClasses;
	}
	public final BooleanProperty condition = new BooleanProperty( this, "condition", null );
	//Rem @Override
	protected void internalSchedule( double t, double dt ) {
		Object conditionValue = condition.get();
		if( conditionValue instanceof Expression ) {
			conditionValue = ((Expression)conditionValue).getValue();
		}
		if( conditionValue != null ) {
			boolean currentValue = ((Boolean)conditionValue).booleanValue();
			if( m_previousValue ) {
				//pass
			} else {
				if( currentValue ) {
					trigger();
				}
			}
			m_previousValue = currentValue;
		} else {
			m_previousValue = false;
		}
		super.internalSchedule( t, dt );
	}
	private boolean m_previousValue;
	//Rem @Override
	protected void started( edu.cmu.cs.stage3.alice.core.World world, double time ) {
		super.started( world, time );
		m_previousValue = false;
	}
}
